package com.zdsoft.crm.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDate;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.util.Date;

import org.springframework.util.StringUtils;

public class DateUtil {

	public static String parseStr(Date date, String format) {
		if (StringUtils.isEmpty(format)) {
			format = "yyyy-MM-dd";
		}
		SimpleDateFormat formatter = new SimpleDateFormat(format);
		return formatter.format(date);
	}

	// 获取本周第一天
	public static LocalDate getFirstDayOfWeek() {
		Instant instant = Instant.now();
		OffsetDateTime instant1 = instant.atOffset(ZoneOffset.ofHours(8));
		// instant.atZone(ZoneId.)
		// 获取今天是本周的第几天
		int dayOfWeek = instant1.getDayOfWeek().getValue();
		return instant1.minusDays(dayOfWeek - 1).toLocalDate();
	}

	// 获取本周最后一天
	public static LocalDate getLastDayOfWeek() {
		Instant instant = Instant.now();
		OffsetDateTime instant1 = instant.atOffset(ZoneOffset.ofHours(8));
		// instant.atZone(ZoneId.)
		// 获取今天是本周的第几天
		int dayOfWeek = instant1.getDayOfWeek().getValue();
		// 获取剩余
		int tmp = 7 - dayOfWeek;
		return instant1.plusDays(tmp).toLocalDate();
	}
	
	public static Date toDate(String dateStr) {
		try {
			return DateFormat.getDateInstance().parse(dateStr);
		} catch (ParseException e) {
			return new Date();
		}
	}

}
